﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace RESTful.DAO
{
    public class DataProvider
    {
        public String ConnectionString = @"Data Source=.\MSSQL;AttachDbFilename=|DataDirectory|\NEWS.mdf;Integrated Security=True;User Instance=True";
        public SqlConnection connection;
        public SqlCommand command;
        public SqlDataAdapter adapter;

        public void Connect()
        {
            connection = new SqlConnection(ConnectionString);
            connection.Open();
        }

        public void DisConnect()
        {
            connection.Close();
        }

        public void ExecuteNonQuery(String sql)
        {
            Connect();
            command = new SqlCommand(sql, connection);
            command.ExecuteNonQuery();
            DisConnect();
        }

        public object ExcuteScalar(String sql)
        {
            command = new SqlCommand(sql, connection);
            return command.ExecuteScalar();
        }

        public DataTable ExecuteQuery(String sql)
        {
            DataSet ds = new DataSet();
            adapter = new SqlDataAdapter(sql, connection);
            adapter.Fill(ds);
            return ds.Tables[0];                        
        }
    } 
}